package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import domain.FanUsers;
import domain.User;

public class FansDaoImpl implements FansDao{
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	//0是删除，1是未删除
	@Override
	public void insertFans(String acc, int fanId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where account = ?");
		q.setString(0, acc);
		List userList = q.list();
		
		User fan = null;
		FanUsers fu = new FanUsers();
		if(userList.size()>=1)
			fan = (User) userList.get(0);
		User faned = null;
		 q = session.createQuery("from FanUsers where fan_id = ? and faned_id =?");
		q = q.setInteger(0, fan.getId());
		 
		 q.setInteger(1, fanId);
		 List listFanU = q.list();
		 if(listFanU.size()>=1){
			 fu = (FanUsers) listFanU.get(0);
		 }else{
		faned = (User) session.get(User.class, fanId);
//		faned.getId();
//		FansUser f
		fu.setFanUser(fan);
		fu.setFanedUser(faned);
		 }
		 fu.setIdDel(1);
		session.saveOrUpdate(fu);
//		faned.getFans().add(fan);
//		fan.getFaned().add(faned);
//		q = session.createQuery("from FanUsers where FAN_ID = ? and FANED_ID=?");
//		q.setInteger(0, fanId);
//		q.setInteger(1, faned.getId());
//		q.list();
	}

	@Override
	public List<FanUsers> showFans(String acc) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where account = ?");
		q.setString(0, acc);
		List userList = q.list();
		User u = null;
		if(userList.size()>=1){
			u = (User) userList.get(0);
			
		}
		q = session.createQuery("from FanUsers where faned_id = ?");
		List<FanUsers> fansU =null;
		if(u!=null){
			q.setInteger(0, u.getId());
		}
		fansU = q.list();
		List<User> fans =  u.getFans();
		if(fans.size()>=1)
			fans.get(0);
		return fansU;
	}

	
	@Override
	public List<FanUsers> showPersonFans(int userId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		User u= (User)session.get(User.class, userId);
		Query q = session.createQuery("from FanUsers where faned_id = ?");
		List<FanUsers> fansU =null;
		if(u!=null){
			q.setInteger(0, u.getId());
		}
		fansU = q.list();
		List<User> fans =  u.getFans();
		if(fans.size()>=1)
			fans.get(0);
		return fansU;
	}
	@Override
	public List<FanUsers> showFaned(String acc) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where account = ?");
		q.setString(0, acc);
		List userList = q.list();
		User u = null;
		if(userList.size()>=1){
			u = (User) userList.get(0);
			
		}
		q = session.createQuery("from FanUsers where fan_id = ? and iddel = ?");
		List<FanUsers> fansU =null;
		if(u!=null){
			q.setInteger(0, u.getId());
			q.setInteger(1, 1);
		}
		fansU = q.list();
		List<User> fans =  u.getFans();
		if(fans.size()>=1)
			fans.get(0);
		return fansU;
	}

	@Override
	public List<FanUsers> showPersonFaned(int userId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		User u= (User)session.get(User.class, userId);
		Query q = session.createQuery("from FanUsers where fan_id = ? and iddel = ?");
		List<FanUsers> fansU =null;
		if(u!=null){
			q.setInteger(0, u.getId());
			q.setInteger(1, 1);
		}
		fansU = q.list();
		List<User> fans =  u.getFans();
		if(fans.size()>=1)
			fans.get(0);
		return fansU;
	}
	
	@Override
	public void deleteFaned(int fanedId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from FanUsers where id = ?");
		q.setInteger(0, fanedId);
		List list = q.list();
		FanUsers fu = null;
		if(list.size()>=1){
			fu = (FanUsers) list.get(0);
		}
		if(fu!=null){
			fu.setIdDel(0);
		}
		session.saveOrUpdate(fu);
	}

}
